//
//  MCUtils.m
//  MCPublicModule
//
//  Created by Mirko on 15/12/23.
//  Copyright (c) 2015年 Mirko. All rights reserved.
//

#import "HSYUtils.h"

const static NSInteger kDevelopHeight = 1334 / 2;
const static NSInteger kDevelopWidth = 750 / 2;

@implementation HSYUtils

+(NSString *) mc_DocumentPath;
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths firstObject];
}

+(BOOL) createFolder:(NSString *) folderPath;
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) {
        return [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return YES;
}


+(NSBundle *) MCPMBundle
{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MCPublicModuleRss" ofType:@"bundle"];
    NSBundle *boundle = [NSBundle bundleWithPath:bundlePath];
    return boundle;
}

+(UIImage *) mcImageWithName:(NSString *) imgName;
{
    NSString *imgPath = [[HSYUtils MCPMBundle] pathForResource:imgName ofType:@"png"];
    UIImage *resultImg = [UIImage imageWithContentsOfFile:imgPath];
    return resultImg;
}

#pragma mark Date Tools
#define kDateFormatterYToS @"yyyy-MM-dd HH:mm:ss"
#define kDataFormatterYToD @"yyyy-MM-dd"

+(NSString *) durationToStringHToS :(double ) duration
{
    
    NSString *tmphh = [NSString stringWithFormat:@"%d",((int)duration)/3600];
    if ([tmphh length] == 1)
    {
        tmphh = [NSString stringWithFormat:@"0%@",tmphh];
    }
    NSString *tmpmm = [NSString stringWithFormat:@"%d",(((int)duration)/60)%60];
    if ([tmpmm length] == 1)
    {
        tmpmm = [NSString stringWithFormat:@"0%@",tmpmm];
    }
    NSString *tmpss = [NSString stringWithFormat:@"%d",((int)duration)%60];
    if ([tmpss length] == 1)
    {
        tmpss = [NSString stringWithFormat:@"0%@",tmpss];
    }
    __autoreleasing NSString *result = [NSString stringWithFormat:@"%@:%@:%@",tmphh,tmpmm,tmpss];
    return result;
}

+(NSString *) durationToStringMToS :(double ) duration
{
    NSString *tmpmm = [NSString stringWithFormat:@"%d",(((int)duration)/60)%60];
    if ([tmpmm length] == 1)
    {
        tmpmm = [NSString stringWithFormat:@"0%@",tmpmm];
    }
    NSString *tmpss = [NSString stringWithFormat:@"%d",((int)duration)%60];
    if ([tmpss length] == 1)
    {
        tmpss = [NSString stringWithFormat:@"0%@",tmpss];
    }
    __autoreleasing NSString *result = [NSString stringWithFormat:@"%@:%@",tmpmm,tmpss];
    return result;
}

+(NSString *) dateIntervalToStringYToS:(NSTimeInterval) interval
{
    NSDate *data = [NSDate dateWithTimeIntervalSince1970:interval];
    return [HSYUtils dateToStringYToS:data];
}

+(NSString *) dateToStringYToS:(NSDate *) dstDate
{
    NSDateFormatter *formatter = [HSYUtils getDataFormatter:kDateFormatterYToS];
    NSString *dateStr = [formatter stringFromDate:dstDate];
    return dateStr;
}

+(NSDate *) stringToDateYToS:(NSString *) dstStr
{
    NSDateFormatter *formatter = [HSYUtils getDataFormatter:kDateFormatterYToS];
    NSDate *date = [formatter dateFromString:dstStr];
    return date;
}

+(NSString *) dateIntervalToStringYToD:(NSTimeInterval) interval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    return [HSYUtils dateToStringYToD:date];
}

+(NSString *) dateToStringYToD:(NSDate *) dstDate
{
    NSDateFormatter *formatter = [HSYUtils getDataFormatter:kDataFormatterYToD];
    NSString *dateStr = [formatter stringFromDate:dstDate];
    return dateStr;
}

+(NSDate *) stringToDateYToD:(NSString *) dstStr
{
    NSDateFormatter *formatter = [HSYUtils getDataFormatter:kDataFormatterYToD];
    NSDate *date = [formatter dateFromString:dstStr];
    return date;
}

+(NSDateFormatter *) getDataFormatter:(NSString *) formatStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatStr];
    return formatter;
}

+ (NSString *)getTimeStrByDate:(NSDate *)date
{
    if (date == nil) {
        return @"";
    }
    __autoreleasing NSMutableString *displayStr = [NSMutableString string];
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSinceDate:date];
    int hours_ago = ((int)time)%(3600*24)/3600;
    int minutes_ago = ((int)time)%(3600*24)%3600/60;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *timeStr = [dateFormatter stringFromDate:date];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    
    [dateFormatter setDateFormat:@"HH"];
    int nowHour = [[dateFormatter stringFromDate:now] intValue];
    int days_ago = ((int)time + 3600*(24-nowHour))/(3600*24);
    
    if (days_ago == 0 && hours_ago == 0 && minutes_ago <= 0) {
        [displayStr appendString: @"刚刚"];
    } else if(days_ago == 0 && hours_ago == 0 && minutes_ago > 0) {
        [displayStr appendString:[NSString stringWithFormat:@"%d",minutes_ago]];
        [displayStr appendString: @"分钟前"];
    } else if (days_ago == 0 && hours_ago > 0) {
        [displayStr appendString:[NSString stringWithFormat:@"今天 %@", timeStr]];
    } else if(days_ago == 1) {
        [displayStr appendString:[NSString stringWithFormat:@"昨天 %@", timeStr]];
    } else if(days_ago == 2) {
        [displayStr appendString:[NSString stringWithFormat:@"前天 %@", timeStr]];
    } else {
        if(dateStr&&dateStr.length>0)
            [displayStr appendString:dateStr];
    }
    return displayStr;
}

+ (NSString *)getTimeStrByTimeInterval:(NSTimeInterval) interval
{
    //    NSDate *pastDate = [NSDate dateWithTimeIntervalSince1970:interval / 1000];
    NSDate *pastDate = [NSDate dateWithTimeIntervalSince1970:interval];
    return [self getTimeStrByDate:pastDate];
}

+ (NSDate *)dateFromString:(NSString *)dateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    return destDate;
}

//bufb
+ (NSString *) getDate{
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSString *  dateStr=[dateformatter stringFromDate:senddate];
    
    return dateStr;
}

+ (NSString *)getWeekDate
{
    
    NSDate * senddate=[NSDate date];
    //获得系统日期
    NSCalendar * cal=[NSCalendar currentCalendar];
    NSUInteger unitFlags=NSCalendarUnitWeekday|NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear;
    NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
    NSInteger year=[conponent year];
    NSInteger month=[conponent month];
    NSInteger day=[conponent day];
    NSInteger weekday = [conponent weekday];
    NSString *wday = nil;
    switch (weekday) {
        case 1:
            wday = @"Sunday";
            break;
        case 2:
            wday = @"Monday";
            break;
        case 3:
            wday = @"Tuesday";
            break;
        case 4:
            wday = @"Wednesday";
            break;
        case 5:
            wday = @"Thursday";
            break;
        case 6:
            wday = @"Friday";
            break;
        case 7:
            wday = @"Saturday";
            break;
            
        default:
            break;
    }
    NSString * nsDateString= [NSString stringWithFormat:@"%4ld年%2ld月%2ld日 %@", (long)year, (long)month, (long)day,wday];
    //    NSString *nsDateString = [NSString stringWithFormat:@"%d %d %@",day,month,wday];
    return nsDateString;
}

+ (NSString *)mStringRemoveLineBreaksWithString:(NSString *)string{
    NSMutableString *mString = [NSMutableString stringWithString:string];
    NSRange range = [mString rangeOfString:@"\r\n"];
    if (range.length != 0) {
        [mString replaceCharactersInRange:range withString:@""];
    }
    return mString;
}

@end


#pragma mark C method
CGSize ScreenSize()
{
    return [UIScreen mainScreen].bounds.size;
}

CGFloat ScreenWidth()
{
    return ScreenSize().width;
}

CGFloat ScreenHeigth()
{
    return ScreenSize().height;
}

CGFloat RelWidth(CGFloat dWidth)
{
    CGFloat relWidth = ScreenWidth() * dWidth / kDevelopWidth;
    return relWidth;
}

CGFloat RelHeight(CGFloat dHeight)
{
    CGFloat relHeight = ScreenHeigth() *dHeight / kDevelopHeight;
    return relHeight;
}

CGRect RelBounds(CGRect dRect)
{
    CGFloat relWidth = RelWidth(dRect.size.width);
    CGFloat relHeight = RelHeight(dRect.size.height);
    return CGRectMake(0, 0, relWidth, relHeight);
}

CGRect RelRect(CGRect dRect)
{
    CGFloat relWidth = RelWidth(dRect.size.width);
    CGFloat relHeight = RelHeight(dRect.size.height);
    CGFloat relX = dRect.origin.x - (dRect.size.width - relWidth) / 2;
    CGFloat relY = dRect.origin.y - (dRect.size.height - relHeight) / 2;
    return CGRectMake(relX, relY, relWidth, relHeight);
}

CGFloat RemainWidth(CGFloat dWidth)
{
    CGFloat cRemainWidth = kDevelopWidth - dWidth;
    CGFloat relWidth = ScreenWidth() - cRemainWidth;
    return relWidth;
}

CGFloat RemainHeight(CGFloat dHeight)
{
    CGFloat cRemainHeight = kDevelopHeight - dHeight;
    CGFloat relHeight = ScreenHeigth() - cRemainHeight;
    return relHeight;
}

CGFloat RelHeightWithSWidth(CGFloat dHeight)
{
    CGFloat relHeight = dHeight * ScreenWidth() / kDevelopWidth;
    return relHeight;
}
